package cn.edu.zjicm.wordsnet_d.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.download.JudgeNetwork;
import cn.edu.zjicm.wordsnet_d.synchronize.FeedBackInterface;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;

import com.umeng.analytics.MobclickAgent;

public class FeedBackActivity extends WordBaseActivity {
	public static void startActivity(Context context) {
		Intent i = new Intent(context, FeedBackActivity.class);
		context.startActivity(i);
	}

	private EditText feedbackEdit, contactEdit;
	private Button commitBtn;
	private ProgressDialog progressDialog;

	private final int FEED_MAX_LEN = 10000;
	private final int CONTACT_MAX_LEN = 100;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("意见反馈");
		setContentView(R.layout.activity_feedback);

		findView();
		initView();
	}

	private void findView() {
		feedbackEdit = (EditText) findViewById(R.id.feed_back_edit);
		contactEdit = (EditText) findViewById(R.id.contactEdit);
		commitBtn = (Button) findViewById(R.id.commitBtn);
	}

	private void initView() {
		feedbackEdit.addTextChangedListener(new MyTextWatcher(feedbackEdit, FEED_MAX_LEN));
		contactEdit.addTextChangedListener(new MyTextWatcher(contactEdit, CONTACT_MAX_LEN));
		commitBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				judgeCommit();
			}
		});
	}

	protected void judgeCommit() {
		if (StringUtil.calculateLength(feedbackEdit.getText().toString()) < 10) {
			Toast.makeText(this, "输入内容少于5个字", Toast.LENGTH_SHORT).show();
		} else if (!JudgeNetwork.getInstance(this).isWifiConnected() && !JudgeNetwork.getInstance(this).isMobileConnected()) {
			Toast.makeText(this, "网络未连接，提交失败，请稍后再试", Toast.LENGTH_SHORT).show();
		} else {
			if (AppPreference.isToadyFeedBack(this, DateTimeUtils.getDate()) && AppPreference.getFeedBackCount(this) >= 5) {
				Toast.makeText(this, "提交次数过多，明天再来", Toast.LENGTH_SHORT).show();
			} else {
				commit();
			}
		}
	}

	private void commit() {
		AppPreference.setFeedBackDate(this, DateTimeUtils.getDate());
		AppPreference.setFeedBackCount(this);
		feedBackHandler.sendEmptyMessage(MSG_START);
		FeedBackInterface.getInstance(this).submitFeekback(feedbackEdit.getText().toString(), contactEdit.getText().toString(), feedBackHandler);
	}

	protected void changeCount() {
		int len = feedbackEdit.getText().length();
		if (len == 0) {
			commitBtn.setEnabled(false);
		} else {
			commitBtn.setEnabled(true);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	class MyTextWatcher implements TextWatcher {

		EditText editText;
		int MAX_COUNT;

		public MyTextWatcher(EditText editText, int MAX_COUNT) {
			this.editText = editText;
			this.MAX_COUNT = MAX_COUNT;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			changeCount();
		}

		@Override
		public void afterTextChanged(Editable s) {
			int editStart = editText.getSelectionStart();
			int editEnd = editText.getSelectionEnd();

			// 先去掉监听器，否则会出现栈溢出
			editText.removeTextChangedListener(this);

			boolean toastHasShow = false;
			// 注意这里只能每次都对整个EditText的内容求长度，不能对删除的单个字符求长度
			while (StringUtil.calculateLength(s.toString()) > MAX_COUNT) { // 当输入字符个数超过限制的大小时，进行截断操作
				s.delete(editStart - 1, editEnd);
				editStart--;
				editEnd--;
				if (!toastHasShow) {
					if (editText == feedbackEdit) {
						Toast.makeText(FeedBackActivity.this, "意见反馈不能超过10000个字符", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(FeedBackActivity.this, "联系方式不能超过100个字符", Toast.LENGTH_SHORT).show();
					}
					toastHasShow = true;
				}
			}
			// mEditText.setText(s);将这行代码注释掉就不会出现后面所说的输入法在数字界面自动跳转回主界面的问题了，多谢@ainiyidiandian的提醒
			editText.setSelection(editStart);

			// 恢复监听器
			editText.addTextChangedListener(this);
		}

	}

	public static final int MSG_START = 0;
	public static final int MSG_SUCCESS = 1;
	public static final int MSG_UNSUCCESS = 2;
	public static final int MSG_BAD_NEWWORK = 3;

	Handler feedBackHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what != MSG_START)
				if (progressDialog != null) {
					progressDialog.dismiss();
					progressDialog = null;
				}
			switch (msg.what) {
			case MSG_START:
				progressDialog = ProgressDialog.show(FeedBackActivity.this, null, "正在提交反馈");
				break;
			case MSG_UNSUCCESS:
				Toast.makeText(FeedBackActivity.this, "提交失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case MSG_BAD_NEWWORK:
				Toast.makeText(FeedBackActivity.this, "网络未连接，提交失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case MSG_SUCCESS:
				Toast.makeText(FeedBackActivity.this, "谢谢您提供的反馈！", Toast.LENGTH_SHORT).show();
				finish();
				break;
			}
		};
	};
}
